
package guiprototype;

import java.lang.Integer;
import javax.swing.JPanel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics; 
import java.awt.event.MouseEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.event.MouseInputListener;

import guiprototype.Area;
import guiprototype.World;


public class MapPanel extends JPanel
{
    World world;

    /** Handler for mouse input on map. TODO: Is it a good design to write multiple such classes for different application states? */
    class MapInputHandler implements MouseInputListener, KeyListener
    {
        public void keyPressed(KeyEvent e) {
        }

        public void keyReleased(KeyEvent e) {
        }

        public void keyTyped(KeyEvent e) {
        }

        public void mouseMoved(MouseEvent e)
        {
            int x = e.getX();
            int y = e.getY();

            // Find closest country (very slow algorithm!)
            int distance = Integer.MAX_VALUE;
            String best = "";
            for(Area c: world.countries.values())
            {
                int d = (c.position.x-x)*(c.position.x-x) + (c.position.y-y)*(c.position.y-y);
                if(d < distance)
                {
                    distance = d;
                    best = c.name;
                }
            }
            setHighlight(best);
        }
        
        public void mousePressed(MouseEvent e) {
            //System.out.println("Pressed country " + highlighted_name);
            setDragged(highlighted_name);
        }

        public void mouseReleased(MouseEvent e) {
            //System.out.println("Released country " + dragging_country);
            setDragged("");
        }

        public void mouseClicked(MouseEvent e) {
            //System.out.println("Clicked country " + highlighted_name);
        }

        public void mouseDragged(MouseEvent e) {
            //System.out.println("Dragging country " + dragging_country);
        }

        public void mouseEntered(MouseEvent e) {
            //System.out.println("Entered.");
        }

        public void mouseExited(MouseEvent e) {
            //System.out.println("Exited.");
            setHighlight("");
        }
    };

    private MapInputHandler map_input_handler;
    private String highlighted_name;
    private String dragging_country;

    public MapPanel(World w) {
        world = w;

        highlighted_name = "";
        dragging_country = "";
        
        setBorder(BorderFactory.createLineBorder(Color.black));
        
        map_input_handler = new MapInputHandler();
        addMouseListener(map_input_handler);
        addMouseMotionListener(map_input_handler);
    }
    
    public void setHighlight(String name)
    {
        highlighted_name = name;
        repaint();
        //repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
    }
    
    public void setDragged(String name)
    {
        dragging_country = name;
        repaint();
        //repaint(squareX,squareY,squareW+OFFSET,squareH+OFFSET);
    }

    public Dimension getPreferredSize() {
        return new Dimension(250,200);
    }
    
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for(Area c: world.countries.values())
        {
            if(c.name == dragging_country)
                g.setColor(Color.BLUE);
            else
                g.setColor(Color.GREEN);
            g.fillRect(c.position.x-10, c.position.y-10, 20, 20);
            
            if(c.name == highlighted_name)
                g.setColor(Color.RED);
            else
                g.setColor(Color.BLACK);
            g.drawString(c.name, c.position.x, c.position.y);
        }
      //g.fillRect(squareX,squareY,squareW,squareH);
      //g.setColor(Color.BLACK);
      //g.drawRect(squareX,squareY,squareW,squareH);
    }
}
